package com.softer.wxzj.common.shiro;

import com.softer.wxzj.common.enums.SessionEnum;
import com.softer.wxzj.entity.SysOperator;
import com.softer.wxzj.entity.SysResource;
import com.softer.wxzj.entity.SysRole;
import com.softer.wxzj.mapper.LoginMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * AuthRealm class
 * @author zcz
 * @date 2019/1/16
 */
@Slf4j
public class AuthRealm extends AuthorizingRealm {
    @Autowired
    private LoginMapper loginMapper;
    /**
     * 统计在线人数
     * juc包下线程安全自增
     */
//    public static final AtomicInteger sessionCount = new AtomicInteger(0);
    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        log.info("--------------授权开始----------------");
        SysOperator sysOperator = (SysOperator) principals.fromRealm(this.getClass().getName()).iterator().next();
        List<String> permissionList = new ArrayList<>();
        List<String> roleNameList = new ArrayList<>();
        //查询角色集合
        Set<SysRole> roleSet = loginMapper.findRoleByUsername(sysOperator.getOperName());
        if (!CollectionUtils.isEmpty(roleSet)) {
            for(SysRole role : roleSet) {
                roleNameList.add(role.getRoleName());
                //查询权限集合
                Set<SysResource> permissionSet = loginMapper.findPermissionByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(permissionSet)) {
                    for (SysResource permission : permissionSet) {
                        permissionList.add(permission.getUrl());
//                        if (StringUtils.isNotEmpty(permission.getUrl())){
//                            permissionList.add(permission.getUrl());
//                        }
//                        if (StringUtils.isNotEmpty(permission.getOperation())){
//                            permissionList.add(permission.getOperation());
//                        }
                    }
                }
//                permissionList.remove("bank_information:Newlyadded");
            }
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //permissionList.add("edit");// 添加权限
        //permissionList.add("");// 添加权限
        info.addStringPermissions(permissionList);
        info.addRoles(roleNameList);
        return info;
    }

    /**
     * 认证
     * @param token
     * @return
     */
    @Override
    public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){
        AuthenticationInfo info = authentication(token);
        return info;
    }

    /**
     * 执行认证
     * @param token
     * @return
     */
    private AuthenticationInfo authentication(AuthenticationToken token){
        SimpleAuthenticationInfo info = null;
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
        SysOperator sysOperator = loginMapper.findSysOperByOperNo(username);
        if (sysOperator == null) {
            return null;
        }
        ByteSource credentialsSalt = ByteSource.Util.bytes(sysOperator.getSalt());
        info = new SimpleAuthenticationInfo(sysOperator, sysOperator.getOperPwd(),credentialsSalt, this.getClass().getName());
        return info;
    }

}
